package com.framework.common.exception;

import com.framework.common.Enums.FrameworkErrorCodeEnum;

import java.text.MessageFormat;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/4/12 22:00
 * @since JDK 1.8
 */
public class BusinessException extends RuntimeException{

    private String                 errorCode     = "-1";

    private String                 errorMessage  = "";

    private FrameworkErrorCodeEnum errorCodeObj;

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }

    public BusinessException(Throwable cause) {
        super(cause);
    }

    public BusinessException(String errorCode, String errorMessage) {
        super(errorMessage);
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
        this.errorCodeObj = FrameworkErrorCodeEnum.getInstanceByCode(errorCode);
    }

    public BusinessException(FrameworkErrorCodeEnum txResult) {
        super(txResult.getMsg());
        this.errorCode = txResult.getCode();
        this.errorMessage = txResult.getMsg();
        this.errorCodeObj = txResult;
    }

    public BusinessException(FrameworkErrorCodeEnum txResult, Object... messages) {
        super(messages == null ? txResult.getMsg() : MessageFormat.format(txResult.getMsg(), messages));
        this.errorCodeObj = txResult;
        this.errorCode = txResult.getCode();
        this.errorMessage = messages == null
                ? txResult.getMsg()
                : MessageFormat.format(txResult.getMsg(), messages);
    }

    public FrameworkErrorCodeEnum getPayErrorCodeObj() {
        return errorCodeObj;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }
}
